﻿Imports DotNetNuke.Entities.Modules
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports DotNetNuke.Services.Localization
Imports Lucene.Net.Search
Imports Lucene.Net.Index

Namespace Aricie.DNN.Modules.LuceneSearch.UI.FilteringControls.Basic
    Public Class DesktopModulesDropDownList
        Inherits SimpleFilteringControl

        Private Shared ReadOnly glbAllDomains As String = "AllDomains"

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                BindLayout()
            End If
        End Sub

        Private Sub BindLayout()

            Dim values As New ArrayList


            'On get tous les desktopmodule Isearchable:
            Dim objDkModoController As New DesktopModuleController()
            Dim arrDesktopModules As ArrayList = objDkModoController.GetDesktopModulesByPortal(PortalSettings.PortalId)
            For Each myDkModule As DesktopModuleInfo In arrDesktopModules
                'seulement ceux qui ont un provider d'indexation lucene
                'If Not (myProviderFactory.getProviderFromModuleName(myDkModule.ModuleName) Is Nothing) Then
                'Seulement ceux qui sont sélectionnés pour etre searchable
                'If (PortalSettings.GetSiteSetting(Me.PortalId, myDkModule.ModuleName) = Constants.MODULEAVAILABLEFORSEARCH) Then 'Lucene.SearchProvider.
                If PortalIndexingDriver.GetCurrent.LucenePortalSettings.IsAvailableForSearching(myDkModule.ModuleName) _
                    Then

                    Dim locItemtext As String = Localization.GetString(myDkModule.ModuleName, Me.LocalResourceFile)
                    Dim newItem As ListItem = New ListItem()
                    newItem.Value = myDkModule.ModuleName
                    If String.IsNullOrEmpty(locItemtext) OrElse locItemtext.ToLower.StartsWith("resx:") Then
                        newItem.Text = myDkModule.FriendlyName
                    Else
                        newItem.Text = locItemtext
                    End If
                    values.Add(newItem)

                End If
                'End If
            Next


            If values.Count <= 1 Then
                optSearchDomain.Visible = False
            Else
                'On rajoute l'option "tous les domaines" s'il y en a plus d'un de disponible 
                Dim _
                    allDomainItem As _
                        New ListItem( _
                                      Localization.GetString(glbAllDomains, _
                                                              Localization.GetResourceFile(Me, MyFileName)), _
                                      glbAllDomains)
                values.Insert(0, allDomainItem)

                'récupérer le nom de recherche sur tout le portail/tous les domaines dans les settings du module de recherche
                'Dim searchModule As ModuleInfo = objModules.GetModuleByDefinition(PortalSettings.PortalId, "Aricie.LuceneSearch")
                'Dim locSettings As System.Collections.Hashtable = objModules.GetModuleSettings(locModule.ModuleID)
                'If locSettings.ContainsKey("NameChoixAllDomains") Then

                '    allDomainItem.Text = locSettings("NameChoixAllDomains")

                'End If


                'TODO: comprendre pourquoi il perd parfois le controle optsearchdomain, imposant ainsi une recompilation avec les deux ligtnes 
                'suivantes commentées, pour pouvoir dse connecter et re-parser la skin et ensuite recompiler avec ces deux lignes décommenter et enfin ça marche...

                optSearchDomain.DataSource = values
                optSearchDomain.DataBind()

            End If


        End Sub

        Public Overrides Property Fields As String
            Get
                Return DnnSearchItemField.ModuleName
            End Get
            Set(value As String)
                'MyBase.Fields = value
            End Set
        End Property

        Public Overrides Property Enabled As Boolean
            Get
                Return optSearchDomain.Enabled
            End Get
            Set(value As Boolean)
                optSearchDomain.Enabled = value
            End Set
        End Property

        Public Overrides Function HasFilter() As Boolean
            Return optSearchDomain.Items.Count >= 1 AndAlso optSearchDomain.SelectedValue <> glbAllDomains
        End Function

        Public Overrides Function ReadFilter(q As Lucene.Net.Search.Query) As Boolean
            If TypeOf q Is TermQuery Then
                Dim objTerm As Term = DirectCast(q, TermQuery).Term()
                If objTerm.Field = Fields Then
                    Dim domainValue As String = BusinessController.GetTermText(objTerm)

                    Dim LI = optSearchDomain.Items.FindByValue(domainValue)
                    If (LI IsNot Nothing) Then
                        LI.Selected = True

                        Return True
                    End If
                End If
            End If
            Return False
        End Function

        Public Overrides Function SetFilter() As Lucene.Net.Search.Query
            Return New TermQuery(New Term(Fields, BusinessController.FormatSearchCriteria(Value)))
        End Function

        Public Overrides ReadOnly Property Value As String
            Get
                Return optSearchDomain.SelectedValue
            End Get
        End Property
    End Class
End Namespace